class xT_AICommand_Cmd_Pickup extends xT_AICommand_Cmd_Move;

//=============================================================================
// Variables: Movement
//=============================================================================
var private Vector                      MoveDestination;
var private Rotator                     DesiredRotation;
var private bool                        bShouldWalk;

//=============================================================================
// Events
//=============================================================================
event Popped()
{
	StopLatentExecution();
	Pawn.ZeroMovementVariables();
}

event Pushed()
{

}

//=============================================================================
// State
//=============================================================================
AUTO state Pickup
{
	function bool ValidTouch(Pawn aPawn)
	{
		local xT_DroppedPickup lPickup;
		local Vector lLocation;
		local Vector lWepLocation;

		// make sure its a live player
		if (!aPawn.bCanPickupInventory || (aPawn.DrivenVehicle == None && aPawn.Controller == None))
		{
			return false;
		}

		// make sure thrower doesn't run over own weapon
		if ( (xT_AIController(Outer).bItemToPickup.Physics == PHYS_Falling) && (aPawn == xT_AIController(Outer).bItemToPickup.Instigator) && (xT_AIController(Outer).bItemToPickup.Velocity.Z > 0) )
		{
			return false;
		}
		lLocation = aPawn.Location;
		lWepLocation = xT_AIController(Outer).bItemToPickup.Location;
		// make sure not touching through wall
		if ( !FastTrace(aPawn.Location, xT_AIController(Outer).bItemToPickup.Location) )
		{
			SetTimer( 0.5, false, nameof(xT_AIController(Outer).bItemToPickup.RecheckValidTouch) );
			return false;
		}

		// make sure game will let player pick me up
		if (WorldInfo.Game.PickupQuery(Pawn, xT_AIController(Outer).bItemToPickup.Inventory.class, xT_AIController(Outer).bItemToPickup))
		{
			return true;
		}
		return false;
	}

	function bool CheckTouching()
	{
		local Pawn P;
		local array<int> K;
		local int li;

		foreach xT_AIController(Outer).bItemToPickup.TouchingActors(class'Pawn', P)
		{
			//P = Pawn(Pawn);
			if( Pawn == P )
			{
				bShouldWalk = false;
				if( ValidTouch(Pawn) )
					return true;//xT_AIController(Outer).bItemToPickup.GiveTo(P);
				return false;
			}
		}
		bShouldWalk = true;
		return false;
	}

Begin:
	if(CheckTouching())
	{
		xT_AIController(Outer).bItemToPickup.GiveTo(Pawn);
		`log("xT_AICommand_Pickup :: Item picked up!!!");
		GoTo('Ending');
	}
	else
	{
		`log("xT_AICommand_Pickup :: xT_AICommand_MoveToPosition  executed!!!");
		if(bShouldWalk)
			GoTo('MoveToItem');
		else
			GoTo('Ending');
	}
MoveToItem:
	xT_AIController(Outer).MoveToPosition(xT_AIController(Outer).bItemToPickup.Location, , true);
	
	GoTo('Begin');
Ending:
	PopCommand(Self);
}
//=============================================================================
// DefaultProperties
//=============================================================================
defaultproperties
{
	bShouldWalk = false
}
